home *** CD-ROM | disk | FTP | other *** search
- Unit DQQField;
-
- Interface
-
- Uses
- Windows,Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- DB, DBTables, StdCtrls;
- Const
- TITLE_HEIGHT = 20;
- D_HEIGHT = 200;
- D_WIDTH = 120;
- Type
- TQueryField = Class;
- { ---------- TDQFieldListBox ---------- }
- TDQFieldListBox = Class (TListBox)
- Private
- Protected
- iOldTopIndex: Integer;
- FQueryField: TQueryField;
- Procedure WMHScroll (var Message: TWMHScroll); message WM_HSCROLL;
- Procedure WMVScroll (var Message: TWMVScroll); message WM_VSCROLL;
- Procedure DrawItem (Index: Integer; Rect: TRect; State: TOwnerDrawState); Override;
- Public
- Constructor Create (AOwner: TComponent); Override;
- Destructor Destroy; Override;
- Procedure SetSelectIndex (Index: Integer; bValue: Boolean);
-
- Property QueryField: TQueryField Read FQueryField Write FQueryField;
- Published
- End;
-
- { ---------- TQueryField ---------- }
- TQueryField = Class (TCustomControl)
- Private
- Protected
- StDatabase: String;
- StTable: String;
-
- StMDataBase: String;
- StMTableName: String;
- StMAlias: String;
- StMField: String;
- FMCtrl: TControl;
-
- StDDataBase: String;
- StDTableName: String;
- StDAlias: String;
- StDField: String;
- FDCtrl: TControl;
- FStartMove: Boolean;
-
- ColorTitle: TColor;
- FListBox: TDQFieldListBox;
- OldIndex: Integer;
- FOnReSize: TNotifyEvent;
- FOnAddLink: TNotifyEvent;
-
- Procedure WMGetMinMaxInfo (Var Message: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;
- Procedure WMNCHitTest (Var Message: TWMNCHitTest); message WM_NCHITTEST;
- Procedure WMSize (Var Message: TWMSize); message WM_SIZE;
- Procedure WMMove (Var Message: TWMMove); message WM_MOVE;
- Procedure Paint; Override;
- Procedure ListBoxDrawItem (Control: TWinControl; Index: Integer;
- Rect: TRect; State: TOwnerDrawState);
- Procedure ListBoxDragDrop (Sender, Target: TObject; X, Y: Integer);
- Procedure ListBoxDragOver (Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- Public
- Constructor Create (AOwner: TComponent); Override;
- Destructor Destroy; Override;
- Procedure SetDataBase (p_StDatabase, p_StTable: String);
- Procedure SetDataBaseField (p_StDatabase, p_StTable: String;
- Fields: TStrings);
- Procedure SetListBoxSize;
- Function GetPosition (StField: String): Integer;
-
- Procedure GetField (Items: TStrings);
-
- Property ListBox: TDQFieldListBox Read FListBox;
- Property DataBase: String Read StDatabase Write StDatabase;
- Property TableName: String Read StTable Write StTable;
-
- Property MDataBase: String Read StMDataBase;
- Property MTableName: String Read StMTableName;
- Property MAlias: String Read StMAlias;
- Property MField: String Read StMField;
- Property MCtrl: TControl Read FMCtrl;
-
- Property DDataBase: String Read StDDataBase;
- Property DTableName: String Read StDTableName;
- Property DAlias: String Read StDAlias;
- Property DField: String Read StDField;
- Property DCtrl: TControl Read FDCtrl;
- Published
- Property Font;
- Property Caption;
- Property OnResize: TNotifyEvent Read FOnReSize Write FOnReSize;
- Property OnAddLink: TNotifyEvent Read FOnAddLink Write FOnAddLink;
- Property StartMove: Boolean Read FStartMove Write FStartMove;
- End;
-
-
- Implementation
-
- { ---------- TDQFieldListBox ---------- }
- Constructor TDQFieldListBox.Create (AOwner: TComponent);
- Begin
- inherited Create (AOwner);
- iOldTopIndex := -1;
- FQueryField := nil;
- Style := lbOwnerDrawFixed;
- End;
- Destructor TDQFieldListBox.Destroy;
- Begin
- inherited Destroy;
- End;
- Procedure TDQFieldListBox.WMHScroll (var Message: TWMHScroll);
- Begin
- inherited;
- if FQueryField = nil Then Exit;
- if (iOldTopIndex = -1) or (iOldTopIndex <> TopIndex) Then
- Begin
- TopIndex := iOldTopIndex;
- {$IFDEF WIN32}
- if Assigned (FQueryField.OnResize) Then
- FQueryField.OnResize (FQueryField);
- {$ELSE}
- SendMessage (FQueryField.Handle, WM_SIZE, 0, 0);
- {$ENDIF}
- End;
- End;
- Procedure TDQFieldListBox.WMVScroll (var Message: TWMVScroll);
- Begin
- inherited;
- if FQueryField = nil Then Exit;
- if (iOldTopIndex = -1) or (iOldTopIndex <> TopIndex) Then
- Begin
- {$IFDEF WIN32}
- TopIndex := iOldTopIndex;
- if Assigned (FQueryField.OnResize) Then
- FQueryField.OnResize (FQueryField);
- {$ELSE}
- SendMessage (FQueryField.Handle, WM_SIZE, 0, 0);
- {$ENDIF}
- End;
- End;
- Procedure TDQFieldListBox.SetSelectIndex (Index: Integer; bValue: Boolean);
- Begin
- Items.Objects[Index] := TObject (bValue);
- Invalidate;
- End;
- Procedure TDQFieldListBox.DrawItem (Index: Integer; Rect: TRect; State: TOwnerDrawState);
- Var
- bValue: Boolean;
- Begin
- bValue := Boolean (Items.Objects[Index]);
- if bValue Then
- Begin
- Canvas.Brush.Color := clRed;
- Canvas.Font.Color := clWhite;
- End
- Else
- Begin
- Canvas.Brush.Color := clWhite;
- Canvas.Font.Color := clBlack;
- End;
- Canvas.FillRect(Rect);
- if Index < Items.Count then
- Canvas.TextOut (Rect.Left + 2, Rect.Top, Items[Index]);
- End;
- { ---------- TQueryField ---------- }
- Constructor TQueryField.Create (AOwner: TComponent);
- Begin
- inherited Create (AOwner);
- FStartMove := FALSE;
- ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
- csSetCaption, csDoubleClicks];
- Color := clSilver;
- Height := D_HEIGHT;
- Width := D_WIDTH;
- ColorTitle := clNavy;
- Caption := '';
- FListBox := TDQFieldListBox.Create (Self);
- FListBox.QueryField := Self;
- FListBox.Parent := Self;
- FListBox.Style := lbOwnerDrawFixed;
- FListBox.OnDrawItem := ListBoxDrawItem;
- FListBox.DragMode := dmAutomatic;
- FListBox.OnDragDrop := ListBoxDragDrop;
- FListBox.OnDragOver := ListBoxDragOver;
- OldIndex := -1;
- Visible := FALSE;
- End;
- Destructor TQueryField.Destroy;
- Begin
- FListBox.Free;
- inherited Destroy;
- End;
- Procedure TQueryField.Paint;
- Var
- TRc: TRect;
- iTemp: Integer;
- Begin
- TRc := ClientRect;
- Dec (TRc.Right);
- Dec (TRc.Bottom);
- Canvas.Pen.Color := clSilver;
- Canvas.MoveTo (TRc.Left, TRc.Bottom);
- Canvas.LineTo (TRc.Left, TRc.Top);
- Canvas.LineTo (TRc.Right, TRc.Top);
- Canvas.Pen.Color := clBlack;
- Canvas.LineTo (TRc.Right, TRc.Bottom);
- Canvas.LineTo (TRc.Left, TRc.Bottom);
-
- Inc (TRc.Left);
- Inc (TRc.Top);
- Dec (TRc.Right);
- Dec (TRc.Bottom);
- Canvas.Pen.Color := clWhite;
- Canvas.MoveTo (TRc.Left, TRc.Bottom);
- Canvas.LineTo (TRc.Left, TRc.Top);
- Canvas.LineTo (TRc.Right, TRc.Top);
- Canvas.Pen.Color := clGray;
- Canvas.LineTo (TRc.Right, TRc.Bottom);
- Canvas.LineTo (TRc.Left, TRc.Bottom);
-
- Inc (TRc.Left, 3);
- Inc (TRc.Top, 3);
- Dec (TRc.Right, 2);
- TRc.Bottom := TRc.Top + TITLE_HEIGHT;
-
- Canvas.Brush.Color := ColorTitle;
- Canvas.Font.Assign (Self.Font);
- Canvas.FillRect (TRc);
- Canvas.Font.Color := clWhite;
-
- iTemp := Length (Caption);
- if iTemp > 0 Then
- // PaintTextStr (Canvas, TRc, taCenter, FALSE, Caption);
- DrawText (Canvas.Handle, PChar (Caption), iTemp, TRc,
- DT_CENTER OR DT_VCENTER OR DT_SINGLELINE);
- End;
- Procedure TQueryField.WMGetMinMaxInfo (Var Message: TWMGetMinMaxInfo);
- Begin
- With Message.MinMaxInfo^ Do
- Begin
- ptMinTrackSize.X := 50;
- ptMinTrackSize.Y := TITLE_HEIGHT + 8;
- End;
- Message.Result := 0;
- {Tell windows you have changed minmaxinfo}
- inherited;
- End;
- Procedure TQueryField.WMNCHitTest (Var Message :TWMNCHitTest);
- Var
- pt: TPoint;
- iSpace: Integer;
- Begin
- iSpace := 10;
- pt.x := Message.XPos;
- pt.y := Message.YPos;
- pt := ScreenToClient (pt);
- if (pt.x >= 0) and (pt.x <= iSpace) and (pt.y >= 0) and (pt.y <= iSpace) Then
- Begin
- Message.Result := HTTOPLEFT;
- Exit;
- End;
- if (pt.x >= Width - iSpace) and (pt.x <= Width) and (pt.y >= 0) and (pt.y <= 5) Then
- Begin
- Message.Result := HTTOPRIGHT;
- Exit;
- End;
- if (pt.x >= Width - iSpace) and (pt.x <= Width) and (pt.y >= Height - iSpace) and (pt.y <= Height) Then
- Begin
- Message.Result := HTBOTTOMRIGHT;
- Exit;
- End;
- if (pt.x >= 0) and (pt.x <= iSpace) and (pt.y >= Height - iSpace) and (pt.y <= Height) Then
- Begin
- Message.Result := HTBOTTOMLEFT;
- Exit;
- End;
-
- iSpace := 5;
- if (pt.y >= 0) and (pt.y <= iSpace) Then
- Begin
- Message.Result := HTTOP;
- Exit;
- End;
- if (pt.y >= Height - iSpace) and (pt.y <= Height) Then
- Begin
- Message.Result := HTBOTTOM;
- Exit;
- End;
- if (pt.x >= 0) and (pt.x <= iSpace) Then
- Begin
- Message.Result := HTLEFT;
- Exit;
- End;
- if (pt.x >= Width - iSpace) and (pt.x <= Width) Then
- Begin
- Message.Result := HTRIGHT;
- Exit;
- End;
-
- if (pt.y >= 0) and (pt.y <= TITLE_HEIGHT + 4) Then
- Message.Result := HTCAPTION
- Else
- Message.Result := HTCLIENT;
- Message.Result := HTCAPTION
- End;
- Procedure TQueryField.WMSize (Var Message: TWMSize);
- Begin
- if not FStartMove Then Exit;
- SetListBoxSize;
- if Assigned (FOnResize) Then
- FOnResize (Self);
- End;
- Procedure TQueryField.SetListBoxSize;
- Begin
- FListBox.Left := 4;
- FListBox.Top := TITLE_HEIGHT + 6;
- FListBox.Width := Width - 8;
- FListBox.Height := Height - TITLE_HEIGHT - 10;
- End;
- Procedure TQueryField.WMMove (Var Message: TWMMove);
- Begin
- if FStartMove Then
- if Assigned (FOnResize) Then
- FOnResize (Self);
- End;
- Procedure TQueryField.SetDataBaseField (p_StDatabase, p_StTable: String;
- Fields: TStrings);
- Begin
- StDatabase := p_StDatabase;
- if Fields <> nil Then
- Begin
- FListBox.Items.Assign (Fields);
- FListBox.ItemIndex := 0;
- End;
- SetListBoxSize;
- End;
- Procedure TQueryField.SetDataBase (p_StDatabase, p_StTable: String);
- Var
- Table: TTable;
- i: Integer;
- St: String;
- bFound: Boolean;
- Cursor: TCursor;
- Begin
- StDatabase := p_StDatabase;
- StTable := p_StTable;
- Table := TTable.Create (Self);
- {$IFDEF WIN32}
- Table.SessionName := Session.SessionName;
- {$ENDIF}
-
-
- Table.Databasename := StDatabase;
- Table.TableName := StTable;
-
- Cursor := Screen.Cursor;
- Screen.Cursor := crHourGlass;
- Try
- Table.Active := TRUE;
- FListBox.Items.Clear;
- For i := 0 to Table.FieldCount - 1 Do
- Begin
- St := Table.Fields[i].FieldName;
- bFound := FALSE;
- FListBox.Items.Add (St);
- End;
- Finally
- Screen.Cursor := Cursor;
- Table.Active := FALSE;
- Table.Free;
- FListBox.ItemIndex := 0;
- SetListBoxSize;
- End;
- End;
- Procedure TQueryField.ListBoxDrawItem (Control: TWinControl; Index: Integer;
- Rect: TRect; State: TOwnerDrawState);
- Var
- St: String;
- cl: LongInt;
- Begin
- St := FListBox.Items.Strings[Index];
- if odSelected in State Then
- Begin
- FListBox.Canvas.Brush.Color := clNavy;
- FListBox.Canvas.Font.Color := clWhite;
- End
- Else
- Begin
- FListBox.Canvas.Brush.Color := clWhite;
- FListBox.Canvas.Font.Color := clBlack;
- End;
- FListBox.Canvas.FillRect (Rect);
- FListBox.Canvas.TextOut (Rect.Left + 8, Rect.Top, St);
- End;
- Procedure TQueryField.ListBoxDragDrop (Sender, Target: TObject; X, Y: Integer);
- Var
- LBox: TDQFieldListBox;
- iSelect: Integer;
- QF: TQueryField;
- Begin
- LBox := TDQFieldListBox(Sender);
- iSelect := LBox.ItemAtPos (Point (x, y), TRUE);
- LBox.SetSelectIndex (iSelect, TRUE);
- QF := TQueryField (LBox.Parent);
- StDDataBase := QF.DataBase;
- StDTableName := QF.TableName;
- StDAlias := QF.Caption;
- StDField := QF.ListBox.Items[iSelect];
- FDCtrl := QF;
- LBox := TDQFieldListBox(Target);
- QF := TQueryField (LBox.Parent);
- StMDataBase := QF.DataBase;
- StMTableName := QF.TableName;
- StMAlias := QF.Caption;
- StMField := QF.ListBox.Items[QF.ListBox.ItemIndex];
- LBox.SetSelectIndex (QF.ListBox.ItemIndex, TRUE);
- LBox.Invalidate;
- FMCtrl := QF;
- if Assigned (FOnAddLink) Then
- FOnAddLink (Self);
- End;
- Procedure TQueryField.ListBoxDragOver (Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- Var
- LBox: TDQFieldListBox;
- Begin
- Accept := Source is TDQFieldListBox;
- if Sender = Source Then
- Accept := FALSE;
- if Accept = FALSE Then Exit;
- LBox := TDQFieldListBox (Sender);
- if LBox.Parent is TQueryField Then
- Accept := LBox.Parent is TQueryField;
- End;
- Function TQueryField.GetPosition (StField: String): Integer;
- Var
- iFind: Integer;
- Rc: TRect;
- RcItem: TRect;
- i: Integer;
- St: String;
- Begin
- Rc := Self.BoundsRect;
- Result := Rc.Top + (TITLE_HEIGHT div 2) + 2;
- iFind := -1;
- For i := 0 To FListBox.Items.Count - 1 Do
- Begin
- St := FListBox.Items.Strings[i];
- if St = StField Then
- iFind := i;
- End;
- if iFind < 0 Then Exit;
- if iFind < FListBox.TopIndex Then Exit;
- RcItem := FListBox.ItemRect (iFind);
- Result := FListBox.Top + RcItem.Top + (FListBox.ItemHeight div 2);
- Result := Self.Top + Result;
- End;
- Procedure TQueryField.GetField (Items: TStrings);
- Var
- St: String;
- i: Integer;
- Begin
- Items.Clear;
- For i := 0 To FListBox.Items.Count - 1 Do
- Begin
- St := FListBox.Items.Strings[i];
- Items.Add (St);
- End;
- End;
- End.
-